#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu
{
	char name[10];
	int age;
};
int com_it(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}
int com_by_age(const void*e1, const void* e2)
{
	return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
int com_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}
void swap(char*buf1,char*buf2,int width)
{
	for (int i = 0; i < width; i++)
	{
		char cmp = *buf1;
		*buf1 = *buf2;
		*buf2 = cmp;
		buf1++;
		buf2++;
	}
}
void bubble(void* name, size_t numer, size_t width, int(*cmp)(const void* e1, const void* e2))
{
	int i = 0;
	for (i = 0; i < numer - 1; i++)
	{
		int j = 0;
		for (j = 0; j < numer - 1 - i; j++)
		{
			if (cmp(((char*)name + j * width) ,((char*)name + (j + 1) * width) )>0)
			{
				swap((char*)name + j * width, ((char*)name + (j + 1) * width), width);
			}
		}
	}
}
void test1()
{
	int arr[] = { 1,2,3,5,9,10,23,25,27 };
	int sz = sizeof(arr) / sizeof(arr[0]);
	bubble(arr, sz, sizeof(arr[0]), com_it);
	for (int i = 0; i < sz; i++)
	{
		printf("%d ", arr[i]);
	}
}
void test2()
{
	struct stu s[5] = { {"wangbatian",25},{"longaotain",30},{"xiaoyan",1000},{"tiancai",20},{"xixx",15} };
	int sz = sizeof(s) / sizeof(s[0]);
	bubble(s, sz, sizeof(s[0]), com_by_age);
	

}
void test3()
{
	struct stu s[5] = { {"wangbatian",25},{"longaotain",30},{"xiaoyan",1000},{"tiancai",20},{"xixx",15} };
	int sz = sizeof(s) / sizeof(s[0]);
	bubble(s, sz, sizeof(s[0]), com_by_name);
	printf("****");
}
int main()
{
	/*test1();*/
	/*test2();*/
	test3();
	return 0;
}